/**
 * 字典管理服务
 */
import api from './api';
import { Dictionary } from '../types/dictionary';
import { PageResponse } from '../types/pageType';

/**
 * 根据ID获取字典项
 * @param id 字典项ID
 * @returns 字典项详情
 */
export async function getDictionaryById(id: number): Promise<Dictionary> {
  const response = await api.get(`/api/dataaccess/dictionary/${id}`);
  return response.data;
}

/**
 * 创建新字典项
 * @param dictionary 字典项信息
 * @returns 是否创建成功
 */
export async function createDictionary(dictionary: Dictionary): Promise<boolean> {
  const response = await api.post('/api/dataaccess/dictionary', dictionary);
  return response.data;
}

/**
 * 更新字典项
 * @param dictionary 新的字典项信息
 * @returns 是否更新成功
 */
export async function updateDictionary(dictionary: Dictionary): Promise<boolean> {
  const response = await api.put('/api/dataaccess/dictionary', dictionary);
  return response.data;
}

/**
 * 删除字典项
 * @param id 字典项ID
 * @returns 是否删除成功
 */
export async function deleteDictionary(id: number): Promise<boolean> {
  const response = await api.delete(`/api/dataaccess/dictionary/${id}`);
  return response.data;
}

/**
 * 多条件查询字典项
 * @param dictType 字典类型
 * @param dictCode 字典编码
 * @param status 状态(0-禁用,1-启用)
 * @param pageNum 页码
 * @param pageSize 每页大小
 * @returns 分页查询结果
 */
export async function searchDictionaries(
  dictType?: string,
  dictCode?: string,
  enabled?: number,
  pageNum: number = 1,
  pageSize: number = 10
): Promise<PageResponse<Dictionary>> {
  const response = await api.get('/api/dataaccess/dictionary/search', {
    params: {
      dictType,
      dictCode,
      enabled,
      pageNum,
      pageSize
    }
  });
  return response.data;
}